package com.suixun.filter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.suixun.Constants;


public class HeadImgFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String request_uri = req.getRequestURI();
        String request_host = request.getServerName();// 获得主机名

        // 得到web应用程序的上下文路径
        String ctxPath = req.getContextPath();
        // 去除上下文路径，得到剩余部分的路径
        String uri = request_uri.substring((ctxPath + "/media").length());
        if ((uri.endsWith(".gif") || uri.endsWith(".png") || uri.endsWith(".jpg") || uri
                .endsWith(".jpeg"))) {
            if (uri.endsWith(".gif"))
                resp.setHeader("Content-Type", "image/gif");
            else if (uri.endsWith(".jpg") || uri.endsWith(".jpeg"))
                resp.setHeader("Content-Type", "image/jpeg");
            else if (uri.endsWith(".png"))
                resp.setHeader("Content-Type", "image/png");
            else {
                resp.setHeader("Content-Type", "image/jpeg");
            }
            File file = new File(Constants.getMediaSavePath() + uri);
            if (file.exists()) {
                FileInputStream imputstream = new FileInputStream(file);
                int i = imputstream.available();
                byte data[] = new byte[i];
                // 读数据
                imputstream.read(data);
                OutputStream out = resp.getOutputStream();
                out.write(data);
                out.flush();
                out.close();
                return;
            } else
                chain.doFilter(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    public String getRequestURL(HttpServletRequest request) {
        if (request == null) {
            return "";
        }
        String url = "";
        url = request.getContextPath();
        url = url + request.getServletPath();

        java.util.Enumeration names = request.getParameterNames();
        int i = 0;
        if (!"".equals(request.getQueryString()) || request.getQueryString() != null) {
            url = url + "?" + request.getQueryString();
        }

        if (names != null) {
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                if (i == 0) {
                    url = url + "?";
                } else {
                    url = url + "&";
                }
                i++;

                String value = request.getParameter(name);
                if (value == null) {
                    value = "";
                }

                url = url + name + "=" + value;
                try {
                    // java.net.URLEncoder.encode(url, "ISO-8859");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            // String enUrl = java.net.URLEncoder.encode(url, "utf-8");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return url;
    }
}
